;  This file is part of tornado.
; 
;    tornado is free software: you can redistribute it And/Or modify
;    it under the terms of the GNU General Public License As published by
;    the Free Software Foundation, either version 3 of the License, Or
;    any later version.
; 
;    tornado is distributed in the hope that it will be useful,
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;    MERCHANTABILITY Or FITNESS For A PARTICULAR PURPOSE.  See the
;    GNU General Public License For more details.
; 
;    You should have received a copy of the GNU General Public License
;    along With tornado.  If Not, see <http://www.gnu.org/licenses/>. 2
; 

;
; Include the library header
;
IncludeFile "../library/tornado.pbi"


;
; Client & server use same include
;
XIncludeFile "shared.pbi"


;
; Create server instance
;
*server = createGameServer(#GUID,#GAME_PORT,"Tornado session server",#VERSION,#MAX_PLAYER,"PaS")

If *server 
  
  OpenConsole()
  
  PrintN("Server run on the port "+Str(#GAME_PORT))
  
  ;
  ; Server main
  ;
  While #True 
    
    keyPressed$ = Inkey()
    
    ;
    ; Run the server
    ;
    gameServerRun(*server)
    
    ;
    ; Check for event
    ;
    networkEvent.i = serverEvent(*server)
    
    ;
    ; An event has been fired
    ;
    If networkEvent <> #TORNADO_EVENT_NONE
      ;
      ; From who ? ( slot number , start at 0 and go to #MAX_PLAYER-1 )
      ;
      slot.l         = serverEventSlot(*server)
      nickName.s     = Space(2) 
      serverGetClientNickname(*server,slot,@nickName)
      
      ;
      ; Retrieve client nickname
      ;
      serverGetClientNickname(*server,slot,@nickName)
      
      ;
      ; Check event type
      ;
      Select networkEvent
        Case #TORNADO_EVENT_PLAYER_CONNECTED
          PrintN(nickName + " join server")
        Case #TORNADO_EVENT_PLAYER_DISCONNECTED
          PrintN(nickName + " leave server")
        Case #TORNADO_EVENT_PLAYER_CHANGE_NICKNAME
          PrintN(nickName + " is new nickname")
        Case #TORNADO_EVENT_LOOSE_CONNECTION
          PrintN(nickName + " loose connection")
      EndSelect
      
    EndIf 
    
    
    ;
    ; Check if packet is queued ( doesn't fire an event )
    ; 
    If serverPacketQueued(*server) = #True 
      
      ;
      ; Get the packet
      ;
      *packetReceived   = serverGetReceivedPacket(*server)
      messageReceived.s = Space(512) ; allocate some space
      
      ;
      ; Pop the message into variable
      ;
      popPacket(*packetReceived,@messageReceived)
      
      ;
      ; From who ?
      ;
      fromSlot.l = getPacketFrom(*packetReceived)
      
      nickName.s = Space(512)
      
      serverGetClientNickname(*server,fromSlot,@nickName)
      
      PrintN(nickName+" has send : "+messageReceived)
      
      ;
      ; Remove the packet !
      ;
      freePacket(*packetReceived)
      
      
      ;
      ; Re-send the message at all player exept fromSlot
      ;
      For i = 0 To #MAX_PLAYER-1
        If i<>fromSlot
          *packet = createPacket() ; Create packet
          Message.s = nickName+" has send : "+messageReceived ; store message
          pushPacket(*packet,@Message,#TYPE_STRING) ; push message into the packet
          serverSetPacketDestination(*server,*packet,i) ; set destination packet
          serverAddPacket(*packet,*server)
        EndIf 
      Next 

    EndIf 
    
    ;
    ; Send message every 1 sec
    ;
    If messageTimer < ElapsedMilliseconds()
      messageTimer = ElapsedMilliseconds() + 1000
      
      ;PrintN("Send message to all players.")
      
      ;
      ; Create packet
      ;
      *packet = createPacket()
      Message.s = "This is a message test from server !"
      
      ;
      ; Push message into the packet
      ;
      pushPacket(*packet,@message,#TYPE_STRING)
      
      ;
      ; Set packet destination (-1 for all else slot ( 0 to #MAX_PLAYER-1)
      ;
      serverSetPacketDestination(*server,*packet,-1)
      
      ;
      ; Add packet to the server
      ;
      serverAddPacket(*packet,*server)
      
      ; Do not remove *packet with freePacket() , the server
      ; will erase it when he was sended to the client(s) !
      
    EndIf 
    
    ;
    ; Disconnect server with escape key
    ;
    If Asc(keyPressed$) = 27
      serverDisconnect(*server)
      Break 
    EndIf 
    
    Delay(10)
  Wend 
  
  End 

  
Else 
  MessageRequester("Error","Cant create the server, port "+Str(#GAME_PORT)+ " in use ?")
  End 
EndIf 

; IDE Options = PureBasic 5.20 beta 6 (Windows - x86)
; CursorPosition = 19
; FirstLine = 6
; EnableXP